<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input :disabled="formDisabled" v-model="model.name" placeholder="请输入岗位名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="岗位代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input :disabled="formDisabled" v-model="model.code" placeholder="请输入岗位代码" ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="岗位序列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionCategory">
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.positionCategory" dictCode="position_category" placeholder="请选择岗位序列" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sortNo">
              <a-input-number
                :disabled="formDisabled"
                :max="99999999"
                :min="0"
                v-model="model.sortNo"
                placeholder="请输入排序号"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否虚拟岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isVirtual">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.isVirtual"
                dictCode="YES_OR_NO"
                placeholder="请选择是否是虚拟岗位"
                @input="handleIsVirtualInput" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.isVirtual == '1'">
            <a-form-model-item label="岗位下添加人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAddUser">
              <j-dict-select-tag :disabled="formDisabled" type="radio" v-model="model.isAddUser" dictCode="YES_OR_NO" placeholder="请选择岗位下添加人员" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="重复序号处理方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeatSortHandleWay">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.repeatSortHandleWay" dictCode="repeat_sort_handle_way" placeholder="请选择重复序号处理方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="岗位状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionStatus">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.positionStatus" dictCode="position_status" placeholder="请选择岗位状态" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea :disabled="formDisabled" v-model="model.description" rows="4" placeholder="请输入描述" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
export default {
  name: 'ErpOrgPositionForm',
  components: {},
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
    },
    data () {
      return {
        model: {
          positionStatus: 'enabled',
          repeatSortHandleWay: 'insert'
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          code: [
            { required: true, message: '请输入职务编码' },
            {
              validator: (rule, value, callback) => validateDuplicateValue('sys_position', 'code', value, this.model.id, callback)
            }
          ],
          name: [
            { required: true, message: '请输入岗位名称!' }
          ],
          positionCategory: [
            { required: true, message: '请选择岗位序列!' }
          ],
          isVirtual: [
            { required: true, message: '请选择是否虚拟岗位!' }
          ],
          isAddUser: [
            { required: true, message: '请选择岗位下添加人员!' }
          ],
          sortNo: [
            { required: true, message: '请输入排序号!' },
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
          ]
        },
        url: {
          add: '/api/erpmng/sys/position/add',
          edit: '/api/erpmng/sys/position/edit',
          queryById: '/api/erpmng/sys/position/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        if (record && !record.repeatSortHandleWay) {
          record.repeatSortHandleWay = 'insert'
        }
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      handleIsVirtualInput (val) {
        if (this.model.isVirtual && this.model.isVirtual == '1') {
          this.$set(this.model, 'isAddUser', '0')
        } else {
          this.$set(this.model, 'isAddUser', null)
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      }
    }
  }
</script>
